package se.liu.johfa428.game.objects.levelselection;

import se.liu.oschi129.animation.animationspace.AnimationSpace;
import se.liu.oschi129.world.interfaces.InputAction;
import se.liu.oschi129.world.model.World;
import se.liu.oschi129.world.model.WorldInput;
import se.liu.oschi129.world.objects.WorldMovableObject;

/**
 * Creates the movable character in the level selection menu.
 * @author johfa428
 *
 */
public class ObjectLevelSelectionPlayer extends WorldMovableObject implements InputAction{

	private enum Direction {LEFT, RIGHT, UP, DOWN, NONE};
	
	private Direction direction;

	public ObjectLevelSelectionPlayer(double x, double y) {
		super(Type.SOFT,  AnimationSpace.get("img_char"), true, x, y, 27,23);
		direction = Direction.NONE;
		setImageSpeed(0.3);
		setDepth(-1);		
	}

	public void performInputAction(WorldInput input) {
		if (direction != Direction.NONE)
			return;
		if (input.leftDown())
			direction = Direction.LEFT;
		else if (input.rightDown())
			direction = Direction.RIGHT;
		else if (input.climbDown())
			direction = Direction.UP;
		else if (input.crouchDown())
			direction = Direction.DOWN;
	}

	private void moveCharacter(World world) {
		switch (direction) {
		case LEFT: setHorizontalSpeed(-4); break;
		case RIGHT: setHorizontalSpeed(4); break;
		case UP: setVerticalSpeed(-4); break;
		case DOWN: setVerticalSpeed(4); break;
		}
	}
		
	
	@Override public void performStepAction(World world) {
		
		moveCharacter(world);
		
		if (!world.placeFree(this, getX()+getHorizontalSpeed(), getY() + getVerticalSpeed())) {
			this.setHorizontalSpeed(0);
			this.setVerticalSpeed(0);
			direction = Direction.NONE;
		}
		
		super.performStepAction(world);

	}
}


